const api = require('../../../../config/api.js')
const app = getApp()

Page({
  data: {
    nickName:"",
    namelength:10
  },
  onLoad() {
    this.setData({
      nickName: app.globalData.xuserInfo.nickName ? app.globalData.xuserInfo.nickName : app.globalData.userInfo.nickName
    })
  },
  onUnload:function(){
    qq.navigateTo({
      url: "/pages/mine/profile/profile"
    })
  },

  getvalue:function(e){
    namelength=10-e.detail.value.length
    this.setData({
      nickName: e.detail.value,
      namelength:namelength
    })
  },
  finish:function(){
    let nickName = this.data.nickName
    console.log(nickName)
    if (app.globalData.xuserInfo) {
      app.globalData.xuserInfo.nickName = nickName
    } else {
      app.globalData.xuserInfo = {}
      app.globalData.xuserInfo.nickName = nickName
    }
    qq.request({
      url: api.UploadXUserInfo,
      data: {xuserInfo:{"nickName":nickName}},
      header: {
        'Content-Type': 'application/json',
        'Xworld-Token': app.globalData.token
      },
      method: 'POST',
      success: res => {
        console.log('res success', res)
      },  
      fail: res => {
        console.log('res fail', res)
      }
    })
    qq.navigateTo({
      url: '/pages/mine/profile/profile'　　
    })
  },
  backtoprepage:function(e){
    qq.navigateTo({
      url: '/pages/mine/profile/profile'　　
    })
  }
})